unit frmCustomList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, dxSkinsCore, dxSkinOffice2007Black, cxStyles,
  dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage, DB,
  cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  AdvGlowButton, cxLabel, cxGroupBox, ActnList, DBAccess, Uni, MemDS, DM;

type
  TCustomListFrame = class(TFrame)
    tblList: TUniTable;
    dsList: TUniDataSource;
    ActionList: TActionList;
    actAdd: TAction;
    actEdit: TAction;
    actDel: TAction;
    actRefr: TAction;
    actSave: TAction;
    grpList: TcxGroupBox;
    cxLabel1: TcxLabel;
    btnAdd: TAdvGlowButton;
    btnApply: TAdvGlowButton;
    btnDel: TAdvGlowButton;
    btnRefresh: TAdvGlowButton;
    grdList: TcxGrid;
    grdListView: TcxGridDBTableView;
    grdListLevel1: TcxGridLevel;
    AdvGlowButton1: TAdvGlowButton;
    quList: TUniQuery;
    procedure grdListViewEditValueChanged(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem);
    procedure actSaveUpdate(Sender: TObject);
    procedure actDelUpdate(Sender: TObject);
    procedure actAddExecute(Sender: TObject);
    procedure actSaveExecute(Sender: TObject);
    procedure actRefrExecute(Sender: TObject);
    procedure actEditExecute(Sender: TObject); virtual;
    procedure actEditUpdate(Sender: TObject);
    procedure grdListViewDblClick(Sender: TObject);
  private
    { Private declarations }
  protected
    FDataChanged: boolean;    
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{$R *.dfm}

{ TCustomListFrame }

constructor TCustomListFrame.Create(AOwner: TComponent);
begin
  inherited;
  FDataChanged := False;
end;

procedure TCustomListFrame.grdListViewEditValueChanged(
  Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem);
begin
  FDataChanged := True;
end;

procedure TCustomListFrame.actSaveUpdate(Sender: TObject);
begin
  if actSave.Enabled <> (FDataChanged and (dsList.DataSet.State in [dsEdit, dsInsert])) then
    actSave.Enabled := (FDataChanged and (dsList.DataSet.State in [dsEdit, dsInsert]));
end;

procedure TCustomListFrame.actDelUpdate(Sender: TObject);
begin
  if actDel.Enabled <> ((grdListView.ViewData.RecordCount <> 0) and (grdListView.Focused))then
    actDel.Enabled := ((grdListView.ViewData.RecordCount <> 0) and (grdListView.Focused));
end;

procedure TCustomListFrame.actAddExecute(Sender: TObject);
begin
  FDataChanged := True;
  dsList.DataSet.Append;
  dsList.DataSet.Post;
end;

procedure TCustomListFrame.actSaveExecute(Sender: TObject);
begin
  if dsList.DataSet.State in [dsEdit, dsInsert] then
    dsList.DataSet.Post;
  FDataChanged := False;
end;

procedure TCustomListFrame.actRefrExecute(Sender: TObject);
var
  v_id: variant;
begin
  v_id := Null;
  if dsList.DataSet.Active then
    v_id := dsList.DataSet.Fields[0].Value;

  dsList.DataSet.DisableControls;
  try
    dsList.DataSet.Close;
    dsList.DataSet.Open;
  finally
    if not VarIsNull(v_id) then
      dsList.DataSet.Locate('id', v_id, []);

    dsList.DataSet.EnableControls;
  end;
end;

procedure TCustomListFrame.actEditExecute(Sender: TObject);
begin
//
end;

procedure TCustomListFrame.actEditUpdate(Sender: TObject);
begin
  if actEdit.Enabled <> not (dsList.DataSet.State in [dsEdit, dsInsert]) then
    actEdit.Enabled := not (dsList.DataSet.State in [dsEdit, dsInsert]);
end;

procedure TCustomListFrame.grdListViewDblClick(Sender: TObject);
begin
  actEdit.Execute;
end;

end.
